package com.ruoyi.web.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.db.domain.*;
import com.ruoyi.risk.service.IRiskResultInfoService;
import com.ruoyi.special.model.info.BehaviorReportDetail;
import com.ruoyi.special.model.vo.MemberRiskInfoVO;
import com.ruoyi.special.service.ISpecialCostomInfoService;
import com.ruoyi.special.service.ISpecialCostomUserInfoService;
import com.ruoyi.special.service.ISpecialDataInfoService;
import com.ruoyi.web.service.ISpecialAgencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecialAgencyServiceImpl implements ISpecialAgencyService {

    @Autowired
    ISpecialCostomInfoService specialCostomInfoService;
    @Autowired
    ISpecialCostomUserInfoService specialCostomUserInfoService;
    @Autowired
    IRiskResultInfoService riskResultInfoService;
    @Autowired
    ISpecialDataInfoService specialDataInfoService;

    @Override
    public MemberRiskInfoVO getMemberRiskInfo(Long uid, SysUser sysUser){

        try {
            SpecialCostomInfo costomInfo = specialCostomInfoService.getAccountInfoByDeptId(sysUser.getDeptId());

            if (costomInfo == null || !ObjectUtil.equals(Integer.valueOf(1), costomInfo.getRiskFlag())){
                return null;
            }

            RiskResultInfo riskResultInfo = riskResultInfoService.selectRiskResultInfoByUidAndDataIdAndDataType(uid, costomInfo.getId(), "SPECIAL");
            if (riskResultInfo == null ||ObjectUtil.isEmpty(riskResultInfo)){
                return null;
            }


            String result = riskResultInfo.getResult();
            JSONObject jsonObject = JSON.parseObject(result);

            JSONObject result_detail = jsonObject.getJSONObject("result_detail");
            JSONObject behavior_report_detail = result_detail.getJSONObject("behavior_report_detail");

            BehaviorReportDetail behaviorReportDetail = JSON.parseObject(behavior_report_detail.toJSONString(),BehaviorReportDetail.class);

            MemberRiskInfoVO memberRiskInfoVO = new MemberRiskInfoVO();
            memberRiskInfoVO.setBehaviorReportDetail(behaviorReportDetail);

            return memberRiskInfoVO;
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return null;
    }

    @Override
    public SpecialDataInfo getDataByDeptAndUid(Long uid, Long deptId) {

        SpecialDataInfo specialDataInfo = new SpecialDataInfo();
        specialDataInfo.setUid(uid);
        specialDataInfo.setDeptId(deptId);

        List<SpecialDataInfo> dataInfoList = specialDataInfoService.selectSpecialDataInfoList(specialDataInfo);
        if (CollUtil.isEmpty(dataInfoList)){
            return null;
        }
        return dataInfoList.get(0);
    }

}
